/*
* Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const { X2DBeta } = require("./graphics/X2DInterface");
const { Scr } = require("./XDefine");
const { XTools } = require("./XTools");

class RightMenu {
    static MENU = null;
    static HAVE_RECT = false;
    static Reset(detail) {
        let offx = XTools.MOUSE_POS.x;
        if (offx + RightMenu.MENUW > Scr.logicw) {
            offx -= RightMenu.MENUW;
        }
        RightMenu.MENU = {
            x: offx,
            y: XTools.MOUSE_POS.y,
            detail: detail,
            needClose: false,
            canClose: false,
        };
        RightMenu.HAVE_RECT = false;
    }
    static Append(detail) {
        if (RightMenu.MENU) {
            RightMenu.MENU.detail.push(...detail);
        }
    }
    static TYPE = {
        BUTTON: 0,
        GROUP: 1,
        GAP: 2,
    }
    static Button(icon, name, hootkey, callback) {
        return {
            type: RightMenu.TYPE.BUTTON,
            icon: icon,
            name: name,
            hk: hootkey,
            cb: () => {
                callback();
                RightMenu.close();
            }
        };
    }
    static Group(icon, name, objs) {
        return {
            type: RightMenu.TYPE.GROUP,
            icon: icon,
            name: name,
            open: false,
            group: objs,
        }
    }
    static Gap() {
        return {
            type: RightMenu.TYPE.GAP,
        }
    }
    static close() {
        if (RightMenu.MENU) {
            RightMenu.MENU.needClose = true;
        }
    }
    static onDraw() {
        if (RightMenu.MENU) {
            this.id_ = 1;
            RightMenu.DrawGroup(RightMenu.MENU.detail, RightMenu.MENU.x, RightMenu.MENU.y);
            if (RightMenu.MENU.needClose) {
                RightMenu.MENU = null;
            }
            RightMenu.HAVE_RECT = true;
        }
    }
    static MENUW = 220;
    static DrawGroup(grp, x, y) {
        let w = RightMenu.MENUW
        let l = 0;
        for (let e of grp) {
            if (e.type != RightMenu.TYPE.GAP) {
                l += 1;
            }
        }
        if (y + l * 32 > Scr.logich) {
            y = Scr.logich - l * 32;
        }
        // ResManager.ANIMA.DrawAnima("编辑器/主界面/右键菜单/框", x, y, 1, 1, 0, 0xffffffff, 0);
        // ResManager.ANIMA.DrawAnima("编辑器/主界面/右键菜单/框", x, y, 1, l * 32 / 4, 0, 0xffffffff, 1);
        // ResManager.ANIMA.DrawAnima("编辑器/主界面/右键菜单/框", x, y + l * 32, 1, 1, 0, 0xffffffff, 2);
        X2DBeta.fillRect(x, y, w, l * 32, 0xff181818);
        X2DBeta.drawRect(x, y, w, l * 32, 0x80ffffff);
        for (let e of grp) {
            e.rect = [x, y, w, 32];
            if (e.on) {
                X2DBeta.fillRect(...e.rect, 0xff3a5986);
            }
            if (e.type == RightMenu.TYPE.GAP) {
                e.rect = [x, y, w, 0];
                X2DBeta.drawLine(x, y, x + w, y, 0xff808080, 2);
                // X2DBeta.drawText("=============", 10, x + w / 2, y + 10, 1, 1, 0, -2, -2, 0xff00ff00);
                // y += 32;
                continue;
            }
            e.id = this.id_;
            this.id_++;
            X2DBeta.drawText(e.id, 14, x + 5 + 9, y + 16, 1, 1, 0, -2, -2, 0xffffffff);
            if (e.icon != null) {
                // ResManager.ANIMA.DrawAnima(e.icon, x + 5 + 9, y + 16, 1, 1, 0, 0xffffffff);
            }
            X2DBeta.drawText(e.name, 14, x + 5 + 18 + 5, y + 16, 1, 1, 0, -1, -2, 0xffffffff);
            if (e.type == RightMenu.TYPE.BUTTON) {
                if (e.hk) {//热键
                    X2DBeta.drawText(e.hk, 14, x + w - 5, y + 16, 1, 1, 0, -3, -2, 0xff808080);
                }
            }
            else if (e.type == RightMenu.TYPE.GROUP) {
                if (e.open) {
                    X2DBeta.drawText("<", 14, x + w - 5, y + 16, 1, 1, 0, -3, -2, 0xffffffff);
                    RightMenu.DrawGroup(e.group, x + w, y);
                }
                else {
                    X2DBeta.drawText(">", 14, x + w - 5, y + 16, 1, 1, 0, -3, -2, 0xffffffff);
                }
            }
            y += 32;
        }
    }
    static Touch(msg, x, y) {
        if (RightMenu.MENU && RightMenu.HAVE_RECT) {
            if (RightMenu.TouchGroup(RightMenu.MENU.detail, msg, x, y)) {
                return true;
            }
            else if (msg != 2) {
                if (msg != 3) {
                    RightMenu.MENU.canClose = true;
                }
                if (RightMenu.MENU.canClose) {
                    RightMenu.MENU.needClose = true;
                }
            }
            // if (!RightMenu.TouchGroup(RightMenu.MENU.detail, msg, x, y) || RightMenu.MENU.needClose) {
            //     if (msg != 2) {
            //         RightMenu.MENU = null;
            //     }
            // }
            // return true;
        }
        return false;
    }
    static TouchGroup(grp, msg, x, y) {
        for (let e of grp) {
            e.on = false;
        }
        for (let e of grp) {
            if (XTools.InRect(x, y, ...e.rect)) {
                if (e.type == RightMenu.TYPE.GROUP && msg == 1) {
                    e.open = !e.open;
                }
                if (e.type == RightMenu.TYPE.GAP) {

                }
                if (e.type == RightMenu.TYPE.BUTTON) {
                    if (msg == 1) {
                        e.cb();
                    }
                }
                e.on = true;
                return true;
            }
            if (e.type == RightMenu.TYPE.GROUP) {
                if (e.open && RightMenu.TouchGroup(e.group, msg, x, y)) {
                    return true;
                }
            }
        }
        return false;
    }
    static Key(k) {
        if (RightMenu.MENU) {
            if (RightMenu.KeyGroup(RightMenu.MENU.detail, k, 0)) {
                return true;
            }
            else if (k == "Escape") {
                RightMenu.MENU.needClose = true;
            }
            // if (!RightMenu.TouchGroup(RightMenu.MENU.detail, msg, x, y) || RightMenu.MENU.needClose) {
            //     if (msg != 2) {
            //         RightMenu.MENU = null;
            //     }
            // }
            // return true;
        }
        return false;
    }
    // static KEY_SELECT = [-1, -1, -1, -1, -1, -1, -1, -1];
    static KeyGroup(grp, k, deep) {
        let selectp = -1;
        for (let i = 0; i < grp.length; i++) {
            let e = grp[i];
            if (e.on) {
                selectp = i;
                e.on = false;
            }
            if (parseInt(k) == grp[i].id) {
                grp[i].cb();
                return true;
            }
            if (e.type == RightMenu.TYPE.GROUP) {
                if (e.open && RightMenu.KeyGroup(e.group, k, deep + 1)) {
                    return true;
                }
            }
        }
        if (selectp == -1) {
            if (deep > 0) {
                return false;
            }
            selectp = 0;
            grp[0].on = true;
            return true;
        }
        if (k == "ArrowUp") {
            if (grp[selectp].type == RightMenu.TYPE.GROUP) {
                grp[selectp].open = false;
            }
            while (true) {
                selectp--;
                if (selectp < 0) {
                    selectp = grp.length - 1;
                }
                if (grp[selectp].type == RightMenu.TYPE.GAP) {
                    continue;
                }
                // if (grp[selectp].type == RightMenu.TYPE.GROUP) {
                //     grp[selectp].open = true;
                // }
                grp[selectp].on = true;
                return true;
            }
        }
        if (k == "ArrowDown") {
            if (grp[selectp].type == RightMenu.TYPE.GROUP) {
                grp[selectp].open = false;
            }
            while (true) {
                selectp++;
                if (selectp >= grp.length) {
                    selectp = 0;
                }
                if (grp[selectp].type == RightMenu.TYPE.GAP) {
                    continue;
                }
                // if (grp[selectp].type == RightMenu.TYPE.GROUP) {
                //     grp[selectp].open = true;
                // }
                grp[selectp].on = true;
                return true;
            }
        }
        if (k == "Enter" || k == " ") {
            if (grp[selectp].type == RightMenu.TYPE.GROUP) {
                grp[selectp].open = !grp[selectp].open;
            }
            //         if (e.type == RightMenu.TYPE.GAP) {

            //         }
            if (grp[selectp].type == RightMenu.TYPE.BUTTON) {
                grp[selectp].cb();
            }
            return true;
        }
        // if (e.type == RightMenu.TYPE.GROUP) {
        //     if (e.open && RightMenu.TouchGroup(e.group, msg, x, y)) {
        //         return true;
        //     }
        // }
        return false;
    }
}

// RightMenu.Reset([
//     RightMenu.Button(icon,"a", null, () => { }),
//     RightMenu.Group(icon,"b", [
//         RightMenu.Button(null, "c", null, () => { }),
//     ])
// ]);

module.exports = {
    RightMenu
}
